package com.murray.assignment.controller;

import com.murray.assignment.pojo.*;
import com.murray.assignment.service.AddressService;
import com.murray.assignment.service.CartService;
import com.murray.assignment.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author Murray
 * @Date 2021/8/13
 * @Time 7:44
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    AddressService addressService;

    @Autowired
    CartService cartService;

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/submitOrder", method = RequestMethod.GET)
    public ModelAndView submitOrder(@RequestParam(value = "skuIdStr") String skuIdStr, HttpSession session) {
        if (session.getAttribute("user") != null) {
            System.out.println("接收到了");
            Users users = (Users) session.getAttribute("user");
//            //1查询该用户的地址信息
            List<Address> address = addressService.findAddress(users.getUserId());
            System.out.println(users.getUserId() + "是用户id");
//            //2.查询准备购买的商品信息
            Cart cart = cartService.selectCartDetails(users.getCart().getCartId());
            String[] skuIdAttr = skuIdStr.split(",");
            double totalPrice = 0;
//            //存放所有选中的商品的信息
            List<CartDetails> selectedCartDetailsList = new ArrayList<>();
            for (CartDetails cartDetails : cart.getCartDetailsList()) {
                if (Arrays.asList(skuIdAttr).contains(cartDetails.getSkuId().toString())) {
                    selectedCartDetailsList.add(cartDetails);
                    totalPrice += cartDetails.getPrice() * cartDetails.getBuyNum();
                }
            }
            //3.传递到视图进行展示
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("addressList", address);
            modelAndView.addObject("selectedList", selectedCartDetailsList);
            modelAndView.setViewName("Pay");
            modelAndView.addObject("totalPrice", totalPrice);
            System.out.println("跳转");
            return modelAndView;
        }
        //没登录去登录
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping(value = "addOrder")
    @ResponseBody
    public Integer addOrder(@RequestParam("addressId") Integer addressId, @RequestParam("skuId") String skuId, @RequestParam("orderAmount") double orderAmount, HttpSession session) {
        if (session.getAttribute("user") != null) {
            //将接收到的信息组装成order对象
            Orders orders = new Orders();
            orders.setAddressId(addressId);
            Users users = (Users) session.getAttribute("user");
            orders.setUserId(users.getUserId());
            orders.setOrderAmount(orderAmount);
            String[] takeSkuId = skuId.split(",");
            List<OrderDetails> orderDetailsList = new ArrayList<>();
            for (String si : takeSkuId) {
                OrderDetails orderDetails = new OrderDetails();
                orderDetails.setSkuId(Integer.parseInt(si));
                orderDetailsList.add(orderDetails);
            }
            orders.setOrderDetailsList(orderDetailsList);
            users.getCart().setTotalNum(users.getCart().getTotalNum() - takeSkuId.length);
            session.setAttribute("user", users);
            return orderService.addOrder(orders, users.getCart().getCartId());
        }
        return -1;
    }

    /**
     * @return ModelAndView
     * @Description 查询当前用户的所有订单信息
     */
    @RequestMapping(value = "/orderList")
    public ModelAndView orderList(HttpSession session) {
        ModelAndView mv = new ModelAndView();
        if (session.getAttribute("user") != null) {
            Users users = (Users) session.getAttribute("user");
            //查询当前用户的所有订单信息
            List<Orders> ordersList = orderService.selectOrdersByUserId(users.getUserId());
           //传递到视图页
            mv.addObject("ordersList", ordersList);
            mv.setViewName("person/order");
            return mv;
        }
        mv.setViewName("login");
        return mv;
    }


}
